/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.handlers;

import java.util.List;

import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.uml2.uml.InteractionFragment;

import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagram.sequence.sedi.edit.command.AnalyseBestCoveredCommand;
import com.cea.papyrus.diagram.sequence.sedi.edit.command.AnalyseOwnerCommand;
import com.cea.papyrus.diagram.sequence.sedi.edit.command.DecideStopBoundsCommand;
import com.cea.papyrus.diagram.sequence.sedi.edit.command.SelectPartCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.CoverCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.CreateGraphicalElementCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.CreateStopCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.LifelinePosition;
import com.cea.papyrus.diagram.sequence.sedi.model.command.SetOwnerCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Handles creation of StateInvariants.
 * <p>
 * This handler is used from FrameLayoutPolicy.
 * </p>
 * 
 * @author frankd@ifi.uio.no
 */
public class StopCreateHandler extends InteractionFragmentCreateHandler {

	public StopCreateHandler(final EditPolicy hostPolicy) {
		super(hostPolicy);
	}

	@SuppressWarnings("unchecked")
	public Command[] getCreateCommands(final CreateRequest request) {

		boolean isInitialised = initialize(request);
		if (! isInitialised) {
			Debug.log(new Exception("Command initialization error."));
			return null;
		}
				
		// TODO Cleanup method, Update documentation
		// CreateStopCommand [UML]
		// Creates the UML element and adds it to the UML interaction
		// When the command is executed, a GraphicalElement will be
		// created in response to the notification. This creation will
		// consume the Bounds hint set previously
		// AnalyseBestCoveredCommand [SeDi]
		// Analyses the diagram and detects the closest lifeline covered
		// by the new fragment. The result is input to the next command:
		// CoverCommand [UML]
		// Sets the fragment's Covered attribute.
		// AnalyseOwnerCommand [SeDi]
		// Identify owner in the stop's preferred position
		// SetOwnerCommand [UML]

		// TODO Create a combined command that both covers and sets owner

		final CreateStopCommand createCmd = new CreateStopCommand(bestCoveredLifeline, interaction);

		final CreateGraphicalElementCommand createGECmd = new CreateGraphicalElementCommand();
		createGECmd.setAnyEditPart(interactionEP);
		createGECmd.setParentGE(interactionNode);
		
		final AnalyseBestCoveredCommand analyseCoveredCmd = new AnalyseBestCoveredCommand();
		analyseCoveredCmd.setInteractionEditPart(interactionEP);
		// glue1: fragment from createCmd

		// final SetPreferredBoundsHintCommand coverHint = new
		// SetPreferredBoundsHintCommand();
		final DecideStopBoundsCommand decideBoundsCmd = new DecideStopBoundsCommand();
		decideBoundsCmd.setAnyEditPart(interactionEP);
		// glue2: lifeline from analyseCoveredCmd

		final CoverCommand coverCmd = new CoverCommand();
		// glue2: covered from analyseCoveredCmd
		// glue1: fragment from createCmd

		final AnalyseOwnerCommand analyseOwnerCmd = new AnalyseOwnerCommand();
		analyseOwnerCmd.setAnyEditPart(interactionEP);
		analyseOwnerCmd.setDiagram(LookForElement.getDiagram(interactionNode));
		// glue3: location from decideBoundsCmd

		final SetOwnerCommand setOwnerCmd = new SetOwnerCommand();
		// glue1: fragment from createCmd
		// glue4: owner

		final SelectPartCommand selectCmd = new SelectPartCommand();
		selectCmd.setAnyEditPart(interactionEP.getRoot());
		selectCmd.setSelectForEdit(false);

		final Command glueGE = new Command() {
			@Override
			public void execute() {
				createGECmd.setElement(createCmd.getStop());
			}
		};

		final Command glue1 = new Command() {
			@SuppressWarnings("unchecked")
			public void execute() {
				InteractionFragment stop = createCmd.getStop();
				coverCmd.setFragment(stop);
				setOwnerCmd.setFragment(stop);

				GraphNode stopView = 
					ModelUtil.getGraphNode(stop, diagram);
				analyseCoveredCmd.setFragmentNode(stopView);
			}
		};

		final Command glue2 = new Command() {
			public void execute() {
				List<LifelinePosition> covered = analyseCoveredCmd.getCovered();
				coverCmd.setFragment(createCmd.getStop());
				coverCmd.setCovered(covered);

				assert (!covered.isEmpty());
				decideBoundsCmd.setLifeline(covered.get(0).getLifeline());
				decideBoundsCmd.setStop(createCmd.getStop());
				// coverHint.setEditPart(host);
				// coverHint.setFragment(createCmd.getStop());
				// coverHint.setCovered(analyseCoveredCmd.getCovered());

				selectCmd.setTarget(createCmd.getStop());
			}
		};

		final Command glue3 = new Command() {
			public void execute() {
				analyseOwnerCmd.setLocation(decideBoundsCmd
						.getPreferredBounds().getLocation());
				
				
				/*
				 * InteractionFragment stop = createCmd.getStop();
				 * GraphicalElement<InteractionFragment> stopView =
				 * (GraphicalElement<InteractionFragment>)
				 * interactionView.getDiagram().getViewFor(stop);
				 * analyseOwnerCmd.setLocation(stopView.getBounds().getLocation());
				 */
			}
		};

		final Command glue4 = new Command() {
			public void execute() {
				setOwnerCmd.setOwner(analyseOwnerCmd.getOwner());
			}
		};

		return new Command[] { createCmd, glueGE, createGECmd, glue1,
				analyseCoveredCmd, glue2, decideBoundsCmd, coverCmd, glue3,
				analyseOwnerCmd, glue4, setOwnerCmd, selectCmd };
	}
}
